- /* dbl2bdec.cpp by K.Tsuru */
- /*************************************************
- For |x| < BRADIX double x --> SDecimal array
- In "snum.h" FigBlock is defined by
- typedef NCBlock<fType> FigBlock;
- Associate function for SDouble and SDecimal classes
- **************************************************/
- #ifndef SN_H
- #include "sn.h"
- #endif
-
- int doubleToBinDec(double x, FigBlock& res, uint *size){
- if(res.size() == 0){
- *size = 0; return -2;
- }
- // x == 0.0 ?
- if(x == 0.0){
- res.clear();
- *size = 1u;
- return 0;
- }
- double dp = fabs(x);
- if(dp >= BRADIX) return -1;
- int e;
-
- frexp(dp, &e); // dp = y*2^e
- uint sz = (abs(e)+DOUBLE_BITS)/BRADIX_BITS + 2u;
- if( sz > res.size() ) sz = res.size();
- res.clear();
- uint j = 0;
- int f = 0;
- // can be converted exactly to binary
- while(dp < 1.0){
- dp *= BRADIX; j++;
- }
- for(; j < sz; j++){
- res[j] = (fType)dp;
- f++;
- dp -= (double)res[j];
- if(dp == 0.0) break;
- dp *= BRADIX;
- }
- *size = min(j+1u, sz);
- if(dp > 0.0) f = -2; // not enough figures
- return f;
- }
dbl2bdec.cpp : last modifiled at 2017/03/13 14:31:56(1,061 bytes)
created at 2016/04/11 11:17:20
The creation time of this html file is 2017/10/07 10:54:15 (Sat Oct 07 10:54:15 2017).